package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import animation.Animator;

import java.awt.Color;
import java.awt.Dimension;
import mover.GameMover;
import mover.SimpleMover;

import data.Player;
import elements.GameCharacter;
import elements.GameElement;

import scaler.Scaler;
import net.miginfocom.swing.MigLayout;

public class OLDPlayerInfoPane extends JPanel {
	private Player p;
	private GameCharacter gc;
	private boolean firstInit = true;
	private Animator scoreAnimator;
	private JLabel playerNameLabel;
	private JPanel placeAnimPanel;
	private JPanel scoreAnimPanel;
	private Scaler scaler;

	/**
	 * Create the panel.
	 */
	public OLDPlayerInfoPane(Player p) {
		setOpaque(false);
		
		this.p = p;

		
		setFocusTraversalKeysEnabled(false);
		setLayout(new MigLayout("", "[grow 50,left][grow 50,right]", "[grow 66][grow 33]"));
		
		playerNameLabel = new JLabel(p.getName());
		playerNameLabel.setForeground(Color.BLACK);
		playerNameLabel.setBackground(Color.LIGHT_GRAY);
		add(playerNameLabel, "cell 0 0 2 1,alignx center,aligny center");
		
		placeAnimPanel = new JPanel();
		placeAnimPanel.setBackground(Color.CYAN);
		add(placeAnimPanel, "cell 0 1,grow");
		
		scoreAnimPanel = new JPanel();
		add(scoreAnimPanel, "cell 1 1,grow");
		
		scaler = new Scaler(new Dimension(100, 40), scoreAnimPanel);
		
		gc = new GameCharacter(GameCharacter.NO_MOVEMENT,scaler, "" + p.getPoints());
		gc.setColor(Color.BLACK);
		gc.setXY((int)(scaler.dim.getWidth()/2 - gc.getWidth()/2),(int) (scaler.dim.getHeight()/2 - gc.getHeight()/2));
		
		this.scoreAnimator = new Animator(scoreAnimPanel, new GameElement[]{gc});
//		this.scoreAnimator.nextFrame();
//	
		
	}
	
	public void updateScore() {
//		GameMover move;
//		int newStartingY;
//		int oldY;
//		GameCharacter newChar;
//		
//		System.out.println("Update Player " + this.p.getName());
//		int currScore = Integer.parseInt(gc.toString());
//		if(currScore == p.getPoints()) return;
//		if(currScore < p.getPoints()) {
//			move = new SimpleMover(0, 5);
//			newStartingY = -10;
//		}
//		else {
//			move = new SimpleMover(0, -5);
//			newStartingY = scoreAnimPanel.getHeight() + 10;
//		}
//		
//		newChar = new GameCharacter(move,scaler, "" + p.getPoints());
//		if(p.getPoints() > 0)newChar.setColor(Color.GREEN);
//		else if (p.getPoints() == 0)newChar.setColor(Color.DARK_GRAY);
//		else newChar.setColor(Color.RED);
//		newChar.setXY(gc.getX(), newStartingY);
//		
//		oldY = gc.getY();
//		
//		gc.setMover(move);
//		
//		scoreAnimator.add(newChar);
//		
//		while(Math.abs(newChar.getY() - oldY)>5 ){
//			scoreAnimator.nextFrame();
//			try {
//				Thread.sleep(10);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//		}
//		
//		scoreAnimator.removeElem(gc);
//		gc = newChar;
//		
		
		gc.setString(p.getPoints() + "");
		this.scoreAnimator.nextFrame();
		
	}
	
	public void iHaveBuzzed(boolean buzzed){
		if(buzzed){
			this.setBorder(new LineBorder(Color.ORANGE, 3, true));
		}else{
			this.setBorder(null);
		}
	}

	public Player getPlayer() {
		return p;
	}
}
